package com.hanhanxiaochong.zhongxiangchuanmei.view.main.act;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.LoginInfoBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.LoginPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.RxTimerUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.ILoginView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/5.
 * Desc   : 登录act
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginView, PlatformActionListener {

    @BindView(R.id.txt_login_way)
    TextView txtLoginWay;
    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;
    @BindView(R.id.txt_yanzheng)
    TextView txtYanzheng;
    @BindView(R.id.et_password)
    EditText etPassword;

    private boolean pwdFlag = true;

    public static Intent createIntent(Context context) {
        return new Intent(context, LoginActivity.class);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new LoginPresenter(this);
    }

    @Override
    protected void init() {
        LoginInfoBean loginInfo = SharedPreferencesUtil.getsInstances(this).getLoginInfo();
        if (loginInfo != null) {
            mPresenter.autoLogin(loginInfo.getToken());
        } else {
            ToastUtils.showToast(ResourcesUtils.getString(R.string.please_login_first));
        }
    }

    @OnClick({R.id.txt_regist, R.id.ll_qq_login, R.id.ll_wechat_login, R.id.ll_alipay_login, R.id.ll_sina_login, R.id.txt_login_way, R.id.txt_yanzheng, R.id.txt_login})
    public void onViewClicked(View view) {
        String password = etPassword.getText().toString().trim();
        String phone = etPhoneNumber.getText().toString().trim();
        switch (view.getId()) {
            case R.id.txt_regist:
                Intent intent = new Intent(this, RegistActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_qq_login:
                authorize(ShareSDK.getPlatform(QQ.NAME));
                break;
            case R.id.ll_wechat_login:
                authorize(ShareSDK.getPlatform(Wechat.NAME));
                break;
            case R.id.ll_alipay_login:
                break;
            case R.id.ll_sina_login:
                authorize(ShareSDK.getPlatform(Wechat.NAME));
                break;
            case R.id.txt_login_way:
                if (!pwdFlag) {
                    pwdFlag = true;
                    txtYanzheng.setVisibility(View.GONE);
                    etPassword.setHint("密码");
                    txtLoginWay.setText("点击使用验证码登录");
                } else {
                    pwdFlag = false;
                    txtYanzheng.setVisibility(View.VISIBLE);
                    etPassword.setHint("验证码");
                    txtLoginWay.setText("点击使用密码登录");
                }
                break;
            case R.id.txt_yanzheng:
                mPresenter.sendMsg(phone, "1");
                RxTimerUtil.interval(1, 60, new RxTimerUtil.IRxNext() {
                    @Override
                    public void doNext(long number) {
                        txtYanzheng.setClickable(false);
                        txtYanzheng.setText(String.format(ResourcesUtils.getString(R.string.repeat_verify_code_hint), 60 - number));
                    }

                    @Override
                    public void doComplete() {
                        txtYanzheng.setClickable(true);
                        txtYanzheng.setText("验证");
                        txtYanzheng.setTextColor(ResourcesUtils.getColor(R.color.pink));
                    }
                });
                break;
            case R.id.txt_login:
                if (pwdFlag) {
                    mPresenter.normalLogin(phone, password, null);
                } else {
                    mPresenter.normalLogin(phone, null, password);
                }
                break;
        }
    }

    private void authorize(Platform platform) {
        platform.setPlatformActionListener(this); // 设置分享事件回调
        platform.SSOSetting(false);  //设置false表示使用SSO授权方式
        platform.showUser(null);//授权并获取用户信息
    }

    @Override
    public void loginSuccess(LoginInfoBean data) {
        ToastUtils.showToast("登录成功");
        SharedPreferencesUtil.getsInstances(this).setLoginInfo(data);
        Intent loginIntent = new Intent(this, MainActivity.class);
        startActivity(loginIntent);
        finish();
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        platform.removeAccount(true); //移除授权状态和本地缓存，下次授权会重新授权
        mPresenter.thirdLogin(platform, hashMap);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        ToastUtils.showToast(throwable.toString());
        //删除可能的授权缓存数据
        platform.removeAccount(true);
    }

    @Override
    public void onCancel(Platform platform, int i) {
        //删除可能的授权缓存数据
        platform.removeAccount(true);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //finish所有activity
        appManager.killAll();
        System.exit(0);
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
